<?php

/*
  +--------------------------------------------------------------------------
  |   Invision Power Board
  |   =============================================
  |   by Matthew Mecham
  |   (c) 2001 - 2006 Invision Power Services, Inc.
  |
  |   =============================================
  |
  |
  +---------------------------------------------------------------------------
  |   > $Date: 2007-05-02 17:29:12 -0400 (Wed, 02 May 2007) $
  |   > $Revision: 959 $
  |   > $Author: BabyWolf $
  +---------------------------------------------------------------------------
  |
  |   > Access the dkm_bank files
  |   > Module written by BabyWolf
  |   > Date started: 07th February 2009
  |
  |	> Module Version Number: 1.0.0
  |   > DBA Checked: Sat 07 Feb 2009
  |   > Quality Checked: Sat 07 Feb. 2009
  +--------------------------------------------------------------------------
 */


if (!defined('IN_IPB')) {
    print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
    exit();
}

class home {
    # Classes
    var $act;
    var $hotPerPage;
    var $defaultPerPage;
    var $pathway;
    var $code;
    var $ipsclass;

    # Others
    var $output = "";
    var $page_title = "";
    var $nav = array();
    var $page = "";
    var $html = "";
    var $attach_pids = array();
    var $carts = array();
    var $class_attach;
    var $recent_topic;
    var $statfunc = "";
    var $catName = array();
    var $navigations = array();
    var $common;
    /* ------------------------------------------------------------------------- */

    // Auto run
    /* ------------------------------------------------------------------------- */

    function auto_run() {
        //-----------------------------------------
        // Require the HTML and language modules
        //-----------------------------------------
        
        $this->init();
        $this->base_url = $this->ipsclass->vars['default_url'];
        $default_url = $this->ipsclass->vars['board_url'];
        $this->base_url_nosess = "{$this->ipsclass->vars['board_url']}/index.{$this->ipsclass->vars['php_ext']}";
        if(intval($this->ipsclass->member['id']) > 0 ){
            $default_url = $this->ipsclass->vars['profile_url'];
        }else{
            $default_url = $this->ipsclass->vars['board_url'];
        }
        //INITs class attach
        require_once( ROOT_PATH . 'sources/classes/attach/class_attach.php' );
        $this->class_attach = new class_attach();
        $this->class_attach->ipsclass = & $this->ipsclass;
        $this->class_attach->attach_post_key = '';
        $this->class_attach->attach_post_key = '';
        $this->class_attach->type = 'post';
        $this->class_attach->init();
        require_once( ROOT_PATH . "sources/portal_plugins/recent_topics.php" );
        $this->recent_topic = new ppi_recent_topics();
        $this->recent_topic->ipsclass =& $this->ipsclass;
        if (USE_MODULES == 1) {
            require ROOT_PATH . "modules/ipb_member_sync.php";
            $this->modules = new ipb_member_sync();
            $this->modules->ipsclass = & $this->ipsclass;
            
        }
        //echo $this->ipsclass->vars['all_product_cat_id'];exit();
        require_once( ROOT_PATH . 'sources/action_public/Common.php' );
        $this->common = new Common();
        $this->common->ipsclass = & $this->ipsclass;
        $this->common->profile_url = $this->ipsclass->vars['profile_url'];
        $this->common->parseLastVisit();
        
        //exit();
        $this->output .= $this->loadEventSlideModule($this->ipsclass->vars['id_enews'], $defaultStart, $this->hotPerPage);
        switch ($this->ipsclass->input['CODE']) {
            case "intro":
                $this->showStaticTextPage($this->ipsclass->vars['id_about_us'], $this->ipsclass->vars['about_id']);//gioi thieu
                break;
            case "contact":
                $this->showStaticTextPage($this->ipsclass->vars['id_about_us'], $this->ipsclass->vars['contact_id']);//lien he
                break;
            case "actajax":
                $this->show_activities_pages();
                return;
                break;
            case "adCart":
                $this->adToCartModule();
                break;
            case "refeshCart":
                $this->loadRefeshCartModule();
                break;
            case "showCart":
                $this->showCartPage();
                break;
                case "payment":
                    $this->paymentPage();
                    break;
            case "view-new-detail":
                $this->showDetailPage();
                break;
            default :
                $this->showDefaultPage();
                break;
        }
        //var_dump($this->ipsclass->member);exit();
        //menu service
        $show_menu_service_module = $this->loadMenuServiceModule();
        $this->output = str_replace("<% HOTLINE %>", $show_menu_service_module, $this->output);
        //show cart
        $show_advertising_module = $this->loadCartModule();
        $this->output = str_replace("<% CART %>", $show_advertising_module, $this->output);
        
        //advertising
        $show_advertising_module = $this->loadAdvertisingModule();
        $this->output = str_replace("<% ADVERTISING %>", $show_advertising_module, $this->output);
        //var_dump($this->common->getCategory($this->ipsclass->vars['all_product_cat_id']));exit();
        $this->ipsclass->print->add_output($this->output);
        $this->ipsclass->print->do_output(array('TITLE' => $this->page_title, 
                                                'JS' => 1, 
                                                'NAV' => '',
                                                'NAVI' => $this->navigations,
                                                'PAGE'=>$this->page));        
    }
   
    /*
     * start load module
     */
    
    function loadNewsModule($type, $start = "", $perPage = "", $loadNews = FALSE, $loadVideo = FALSE, $isNew = FALSE, $loadAllImage = FALSE, $all_parent = -1){
        //echo $type;exit();
        $where = "";
        if(intval($type) <= 0 && !$loadVideo){
            return null;
        }
        if(intval($start) < 0 ){
            $start = 0;
        }
        if($perPage <= 0){
            $perPage = $this->defaultPerPage;
        }
        //echo $all_parent;
        if($all_parent > 0){
            $childs = array();
            $this->ipsclass->DB->build_query( array('select' => 'f.id, f.name',
                                                'from'   => 'forums f',
                                                'where'    => "f.parent_id = {$all_parent}"
                                                ) 
                                        );
            $this->ipsclass->DB->exec_query();
            while($row = $this->ipsclass->DB->fetch_row()){
                $childs[] = $row['id'];
            }
            if(count($childs) <= 0){
                return null;
            }
            $str = implode(",", $childs);
            $where = "t.forum_id IN ( {$str}) and t.approved = 1  order by t.tid DESC";
            //echo $where;
           
        }else{
            $where = "t.forum_id = " . intval($type)  . " and t.approved = 1 ";
        }
        
        
        $this->ipsclass->DB->build_query( array('select' => 'count(t.tid) as count',
                                                'from'   => 'topics t',
                                                'where'    => $where
                                                ) 
                                        );
        $this->ipsclass->DB->exec_query();
        $totalId = $this->ipsclass->DB->fetch_row();
        //var_dump($totalId);exit();
        if($totalId['count'] <= 0){
            return null;
        }
        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => 't.*',
                                                 'from'     => array( 'topics' => 't' ),
                                                 'where'    => $where . "",
                                                 'limit'         => array($start, $perPage),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'p.*',
                                                                                 'from'   => array( 'posts' => 'p' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 'f.*',
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ),
                                                                         2 => array( 'select' => 'pro.*',
                                                                                      'from'   => array( 'product_info' => 'pro' ),
                                                                                      'where'  => 'pro.product_id = t.tid',
                                                                                      'type'   => 'left' )
                                                 )
                                            )
                                        );
        $this->ipsclass->DB->exec_query();
        //$att_new_id = array();
        $temps = array();
        while($row = $this->ipsclass->DB->fetch_row()){
            $row['rewrite_name'] = $this->common->filter_title($row['name']);
            $row['rewrite_title'] = $this->common->filter_title($row['title']);
            $row['rewrite_date'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['post_date']);
            $row['org_price'] = number_format($row['org_price']);
            $row['price'] = number_format($row['price']);
            $temps[] = $row;
        }
        //var_dump($temps);exit();
        $datas_temp = array();

        foreach ($temps as $row) {

            $firstAttachments = $this->common->get_first_attachment($row['pid']);
            if(is_array($firstAttachments)){
                $row['first_attach'] = $firstAttachments;
            }
            if($loadAllImage){
                $images = $this->common->get_all_image($row['post']);
                $attaches = $this->common->get_all_attachment($row['pid']);
                if(is_array($images) && is_array($attaches)){
                    $images = array_merge($images, $attaches);
                    $row['total_images'] = $images;
                }
            }
            $row = $this->common->format_image_urls($row);
            $row['post'] = preg_replace("#\[attachment=(\d+?)\:(?:[^\]]+?)\]#is","",$row['post']); 
            $row['post'] = $this->recent_topic->Bw_CutString($row['post'], $loadNews ? 120 : 900);
            if($type != "-1"){
                $row['title'] = $this->recent_topic->Bw_CutString($row['title'], 38);
            }

            $row['description'] = $this->recent_topic->Bw_CutString($row['description'], 38);
            //$att_new_id[] = $row['pid'];          
            $datas_temp[] = $row;

        }
        unset ($temps);
        $datas = array();
        //var_dump($loadNews) ;exit();
        if($loadNews){
            $page = $this->ipsclass->buildMyPageLinks( array(  'TOTAL_POSS'  => $totalId['count'],
                                                           'PER_PAGE'    => $perPage,
                                                           'CUR_ST_VAL'  => $start,
                                                           'L_SINGLE'    => "",
                                                           'L_MULTI'     => $this->ipsclass->lang['tpl_pages'],
                                                           'BASE_URL'    => "#"
                                                        )
                                                    );
                                                    //var_dump($page);exit();
            $datas['page'] = $page;
        }
        
        //echo $type;
        $datas['type'] = $type;
        $datas['totalData'] = count($datas_temp);
        $datas['datas'] = $datas_temp;
        //var_dump($datas);exit();
        unset ($att_new_id);
        return $datas;

    }
    function loadSubStaticTextModule($mo=array(), $types, $tid, $length){
        $where = " t.forum_id = f.id and f.id = " . intval($types) . "   and t.tid = {$tid} ";
        $page = array();
        $this->ipsclass->DB->build_query( array( 'select'   => 't.*',
                                                 'from'     => array( 'topics' => 't' ),
                                                 'where'    => $where . "  group by concat(t.tid) order by t.tid DESC",
                                                 'limit'         => array(0, 1),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'p.*',
                                                                                 'from'   => array( 'posts' => 'p' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 'f.*',
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )

            );
        $this->ipsclass->DB->exec_query();
        $module = $this->ipsclass->DB->fetch_row();
        
        $temp = $this->class_attach->render_attachments($module['post'], $this->attach_pids);
        $temp = preg_replace("#\[attachment =(\d+?)\:(?:[^\]]+?)\]#is","",$temp); 
        $temp = preg_replace("#\[attachment*\]#is","",$temp); 
        $module['post'] = $this->recent_topic->Bw_CutString($temp, 500);
        
        if ($module['topic_hasattach']) {
            $module['post'] = $this->class_attach->render_attachments($module['post'], $this->attach_pids);
        }
        if($tid == $this->ipsclass->vars['recruitment_id']){
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoRecruitmentPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $link = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['site_title']}</a> &raquo; <span>".$this->ipsclass->lang['new_recruitment_title'] ."</span>";
        }else if($tid == $this->ipsclass->vars['about_id']){
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoAboutPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $link = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['site_title']}</a> &raquo; <span>".$this->ipsclass->lang['about_title'] ."</span>";
        $this->nav = array($this->ipsclass->lang['about_title']);
        }else if($tid == $this->ipsclass->vars['id_services_news']){
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoServicePage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $link = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['site_title']}</a> &raquo; <span>".$this->ipsclass->lang['services_title'] ."</span>";
        $this->nav = array($this->ipsclass->lang['services_title']);
        }
        $module['headerTitle'] = $mo['headerTitle'];
        $module['pageName'] = $mo['pageName'];
        $module['cssClass'] = $mo['cssClass'];
        
        return $this->ipsclass->compiled_templates['skin_home']-> show_sub_static_module($module);
        unset ($var);
    }
    function loadRelatedModule($type, $tid = ""){
        $type = intval($type);
        $tid = intval($tid);
        $where = "";
        if(intval($tid) > 0){
            $where = "t.forum_id = {$type} and t.tid <> {$tid} and t.approved = 1 group by concat(t.tid) order by t.tid DESC, at.attach_id ASC ";
        }else{
            $where = "t.forum_id = {$type} and t.approved = 1 group by concat(t.tid) order by t.tid DESC, at.attach_id ASC ";
        }  

        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => 't.*',
                                                 'from'     => array( 'topics' => 't' ),
                                                 'where'    => $where . "",
                                                 'limit'         => array(0, 10),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'p.*',
                                                                                 'from'   => array( 'posts' => 'p' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                     1 => array( 'select' => 'f.*',
                                                                                  'from'   => array( 'forums' => 'f' ),
                                                                                  'where'  => 'f.id=t.forum_id',
                                                                                  'type'   => 'left' ),
                                                                     2 => array( 'select' => 'at.*',
                                                                                  'from'   => array( 'attachments' => 'at' ),
                                                                                  'where'  => 'at.attach_rel_id=p.pid ',
                                                                                  'limit'  => array(0,1),
                                                                                  'type'   => 'left' )
                                                     )
                                                )

            );
        $this->ipsclass->DB->exec_query();
        $datas = array();
        $temps = array();
        while($row = $this->ipsclass->DB->fetch_row()){
            $row['rewrite_title'] = $this->common->filter_title($row['title']);
            $row['rewrite_date'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['post_date']);
            $temps[] = $row;
        }
        $datas['totalData'] = count($temps);
        $datas['datas'] = $temps;
        return $datas;
    }
    function loadMediaScrollerModule($type, $start = "", $perPage = "", $loadVideo = FALSE){
        if(intval($type) <= 0 && !$loadVideo){
            return null;
        }
        if(intval($start) < 0 ){
            $start = 0;
        }
        if($perPage <= 0){
            //$perPage = $this->defaultPerPage;
            $perPage = 5;
        }
        if($loadVideo){
            $perPage = 5;
        }
        $module = $this->loadNewsModule($type, $start, $perPage, TRUE, FALSE, FALSE, TRUE);
        //var_dump($module);exit();
        $module['pageName'] = "{$this->ipsclass->lang['seoGalleryPage']}{$this->ipsclass->lang['seoPageSuffix']}";
        $module['cssClass'] = "gallery";
        $module['mediaType'] = "gallery";
        $module['headerTitle'] = $this->ipsclass->lang['galaries_title'];
        //echo $this->ipsclass->compiled_templates['skin_home']->show_mediascroller_module($module);;exit();
        return $this->ipsclass->compiled_templates['skin_home']->show_mediascroller_module($module);
    }
    function loadMenuServiceModule(){
        $module = array();
        return $this->ipsclass->compiled_templates['skin_home']->load_menu_service_module($module);
    }
    function loadCartModule(){
        $module = array();
        $carts = $_SESSION['cart'];
        if(!empty ($carts['item'])){
            $module['countItems'] = count($carts['item']);
        }
        //var_dump(count($carts['item']));
        return $this->ipsclass->compiled_templates['skin_home']->load_cart_module($module);
    }
    function loadRefeshCartModule() {
        echo $this->loadCartModule();
        exit();
    }
    function loadAdvertisingModule(){
        $files = $this->common->read_dir($this->ipsclass->vars['flash_diectory_path']."social/");       
        $module['random_flash_path_module'] = "/flash/social/";
        $module['random_flash_file_module'] = $files[array_rand($files)];
        unset ($files);
        return $this->ipsclass->compiled_templates['skin_home']->load_advertising_module($module);
    }
    function loadEventSlideModule($type, $start = 0, $perPage = 5){
        if(intval($type) <= 0 ){
            return null;
        }
        $module = $this->loadNewsModule($type, $start, $perPage, TRUE, FALSE, FALSE, TRUE);
        return $this->ipsclass->compiled_templates['skin_home']->load_event_slide_module($module);
    }
    
    function adToCartModule(){
        //$carts = array();
        $this->carts = $_SESSION['cart'];
        $id = intval($this->ipsclass->input['itemId']);
        $qty = intval($this->ipsclass->input['itemQty']);
        $remove = intval($this->ipsclass->input['itemRemove']);
        
        //var_dump($this->ipsclass->input);exit();
        //echo 
        if($qty <= 0){
            $qty = 1;
        }
        //check id
        $where = "t.tid = {$id}";
        $this->ipsclass->DB->build_query( array('select' => 'count(t.tid) as count',
                                                'from'   => 'topics t',
                                                'where'    => $where
                                                ) 
                                        );
        $this->ipsclass->DB->exec_query();
        $totalId = $this->ipsclass->DB->fetch_row();
        //echo $totalId['count'];
        if($totalId['count'] > 0){
            //var_dump(empty ($this->carts['item']));
            if(!empty ($this->carts['item'])){
                //echo "aaaa";
                $flag = FALSE;
                foreach ($this->carts as $key => $item) {
                    //var_dump($item);
                    if(!empty ($item)){
                        foreach ($item as $key => $value) {
                            if($value['id'] == $id){
                              $flag = TRUE;  
                              //$n_Qty = intval($value['qty']) + intval($qty);
                              if($remove > 0){
                                  //echo "ddddddddddd";
                                  unset ($this->carts['item'][$key]);
                                  break;
                              }else{
                                  
                                  $n_Qty = intval($qty);
                                  $item = array('id'=>$id, 'qty'=>$n_Qty);
                                  $this->carts['item'][$key] = $item;
                                  break;
                              }
                              
                            }
                        }
                        if($flag == TRUE){
                            $_SESSION['cart'] = $this->carts;
                            break;
                        }else{
                            $item = array('id'=>$id, 'qty'=>$qty);
                            $this->carts['item'][] = $item;
                        }
                    }
                    
                }
            }else{
                
                $item = array('id'=>$id, 'qty'=>$qty);
                $this->carts['item'][] = $item;
            }
            
        }
        
        $_SESSION['cart'] = $this->carts;
        
        //var_dump($_SESSION['cart']);
        //exit();
        if($id <=0){
            echo "no";
        }else{
            echo "yes";
        }
        //var_dump($_SESSION['cart']);
        exit();
    }
    function destroyCart(){
        $_SESSION['cart'] = null;
        $this->carts = null;
        unset ($this->carts);
        unset ($_SESSION['cart']);
    }
    /*
     * end load module
     */
    /*
     * start load page
     */
    function showDefaultPage(){
        
        $page = array();
        $module = array();
        $start = intval($this->ipsclass->input['st']) >= 0 ? $this->ipsclass->input['st'] : 0 ;
        $perPage = 2;

        $module = $this->loadNewsModule($this->ipsclass->vars['all_product_cat_id'], $start, $perPage, TRUE, FALSE, TRUE, TRUE, $this->ipsclass->vars['all_product_cat_id']);
        $show_gamenews_module = $this->ipsclass->compiled_templates['skin_home']->show_sub_new_module($module);
        //var_dump($module);exit();
        unset ($module);

        $module['show_gamenews_module'] = $show_gamenews_module;

        $show_news_module = $this->ipsclass->compiled_templates['skin_home']->show_news_module($module);

        $page['show_news_module'] = $show_news_module;
        $page['show_menu_service_module'] = $show_menu_service_module;
        $page['show_advertising_module'] = $show_advertising_module;
//        $page['show_activity_scroller_module'] = $show_activity_scroller_module;
        $this->output .= $this->ipsclass->compiled_templates['skin_home']->show_default_page($page);
        $this->output .= $this->showOrderPage();
        unset ($page);
        //$module['show_socialnews_module'] = "";
    }
    function showStaticTextPage($types, $tid){
        $where = " t.forum_id = f.id and f.id = " . intval($types) . "   and t.tid = {$tid} ";
        $page = array();
        $this->ipsclass->DB->build_query( array( 'select'   => 't.*',
                                                 'from'     => array( 'topics' => 't' ),
                                                 'where'    => $where . "  group by concat(t.tid) order by t.tid DESC",
                                                 'limit'         => array(0, 1),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'p.*',
                                                                                 'from'   => array( 'posts' => 'p' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 'f.*',
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )

            );
        $this->ipsclass->DB->exec_query();
        $page = $this->ipsclass->DB->fetch_row();
        if ($page['topic_hasattach']) {
            $page['post'] = $this->class_attach->render_attachments($page['post'], $this->attach_pids);
        }
        if($tid == $this->ipsclass->vars['recruitment_id']){
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoRecruitmentPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $nav_path = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['site_title']}</a> &raquo; <span>".$this->ipsclass->lang['new_recruitment_title'] ."</span>";
            $this->page_title = $this->ipsclass->lang['new_recruitment_title'];
            $this->nav = array($this->ipsclass->lang['new_recruitment_title']);
        }else if($tid == $this->ipsclass->vars['about_id']){
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoAboutPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $nav_path = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['site_title']}</a> &raquo; <span>".$this->ipsclass->lang['about_title'] ."</span>";
            $this->page_title = $this->ipsclass->lang['about_title'];
            $this->nav = array($this->ipsclass->lang['about_title']);
        }else if($tid == $this->ipsclass->vars['contact_id']){
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoContactPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $nav_path = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}' title='{$this->ipsclass->lang['seoPumperPrefixTitle']}{$this->ipsclass->lang['home_title']}'>{$this->ipsclass->lang['site_title']}</a> &raquo; <span>".$this->ipsclass->lang['contact_title'] ."</span>";
            $this->page_title = $this->ipsclass->lang['contact_title'];
            $this->nav = array($this->ipsclass->lang['contact_title']);
        }
        $link = $nav_path;
        $page['pathWay']= $this->ipsclass->compiled_templates['skin_home']->load_pathway_module($link);
        $this->output .= $this->ipsclass->compiled_templates['skin_home']->show_static_page($page);
        $this->navigations = array('gt' => 'active');
        //unset ($var);
    }
    function showNewsPage($type){
        $start = intval($this->ipsclass->input['st']) >= 0 ? $this->ipsclass->input['st'] : 0 ;
        $perPage = $this->defaultPerPage;
        $page = $this->loadNewsModule($type, $start, $perPage, FALSE, FALSE, TRUE, TRUE);
        if($type == $this->ipsclass->vars['id_gnews']){
            $module_title= $this->ipsclass->lang['games_title'];
            $page['pageName'] = "{$this->ipsclass->lang['seoGameNewPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $page['seoPageName'] = $this->ipsclass->lang['seoGameNewPage'];
            $page['headerTitle'] = $this->ipsclass->lang['games_title'];
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoGameNewPage']}{$this->ipsclass->lang['seoPageSuffix']}";
        }else if($type == $this->ipsclass->vars['id_snews']){
            $module_title= $this->ipsclass->lang['socials_title'];
            $page['pageName'] = "{$this->ipsclass->lang['seoSocialNewPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $page['seoPageName'] = $this->ipsclass->lang['seoSocialNewPage'];
            $page['headerTitle'] = $this->ipsclass->lang['socials_title'];
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoSocialNewPage']}{$this->ipsclass->lang['seoPageSuffix']}";
        }else if($type == $this->ipsclass->vars['id_top_videos']){
            $module_title= $this->ipsclass->lang['video_title'];
            $page['pageName'] = "{$this->ipsclass->lang['seoVideoPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $page['seoPageName'] = $this->ipsclass->lang['seoVideoPage'];
            $page['headerTitle'] = $this->ipsclass->lang['video_title'];
            $current_path = $this->ipsclass->vars['home_url'] . "/$this->ipsclass->lang['seoVideoPage']}{$this->ipsclass->lang['seoPageSuffix']}";
        }else if($type == $this->ipsclass->vars['id_services_news']){
            $module_title= $this->ipsclass->lang['service_title'];//service_title
            $page['pageName'] = "{$this->ipsclass->lang['seoServicePage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $page['seoPageName'] = $this->ipsclass->lang['seoServicePage'];
            $page['headerTitle'] = $this->ipsclass->lang['service_title'];
            $current_path = $this->ipsclass->vars['home_url'] . "/{$this->ipsclass->lang['seoServicePage']}{$this->ipsclass->lang['seoPageSuffix']}";
        }else if($type == $this->ipsclass->vars['id_action_news']){
            $module_title= $this->ipsclass->lang['picture_activities_title'];
            $page['seoPageName'] = $this->ipsclass->lang['seoGalleryPage'];
            $page['headerTitle'] = $this->ipsclass->lang['service_title'];
            $current_path = "";
        }else if($type == $this->ipsclass->vars['id_enews']){
            $module_title= $this->ipsclass->lang['event_title'];
            $page['seoPageName'] = $this->ipsclass->lang['seoEventPage'];
            $page['headerTitle'] = $this->ipsclass->lang['event_title'];
            $current_path = "";
        }

        $link = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang["seoPumperHomeTitle"]}' title='{$this->ipsclass->lang["seoPumperHomeTitle"]}' >{$this->ipsclass->lang['home_title']}</a> &raquo; <span>{$module_title}</span>";;
        $page['pathWay'] = $this->ipsclass->compiled_templates['skin_home']->load_pathway_module($link);
        $this->output .=  $this->ipsclass->compiled_templates['skin_home']->skin_sub_page($page);
    }
    function showMediaPage($type){
        $start = intval($this->ipsclass->input['st']) >= 0 ? $this->ipsclass->input['st'] : 0 ;
        $perPage = $this->defaultPerPage;
        $page = array();
        $page['isVideo'] = FALSE;
        if($type == $this->ipsclass->vars['id_top_videos']){
            $page = $this->loadNewsModule(-2, $start, $perPage, FALSE, TRUE);
            $this->page = $this->ipsclass->lang['video_title'];
            $module_title = $this->ipsclass->lang['video_title'];
            $page['pageName'] = "{$this->ipsclass->lang['seoVideoPage']}{$this->ipsclass->lang['seoPageSuffix']}";
            $page['headerTitle'] = $this->ipsclass->lang['video_title'];
            $page['type'] = $this->ipsclass->vars['id_top_videos'];
        }
        $page['c'] = $this->code;
        $page['act'] = $this->act;
        $link = "<a href = '{$this->ipsclass->vars['default_url']}' alt='{$this->ipsclass->lang["seoPumperHomeTitle"]}' title='{$this->ipsclass->lang["seoPumperHomeTitle"]}' >{$this->ipsclass->lang['home_title']}</a> &raquo; <span>{$module_title}</span>";
        $page['pathWay']= $this->ipsclass->compiled_templates['skin_home']->load_pathway_module($link);
        $this->output .=  $this->ipsclass->compiled_templates['skin_home']->show_media_page($page);
    }
    function showDetailPage(){
        require_once( ROOT_PATH . "sources/portal_plugins/recent_topics.php" );
        $this->recent_topic = new ppi_recent_topics();
        $this->recent_topic->ipsclass =& $this->ipsclass;
        $topic_id = intval(trim($this->ipsclass->input['id']));
        //var_dump($this->ipsclass->input['id']);exit();
        $where = "t.tid = " . $topic_id . " and t.approved = 1"; // and post <> 0
        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => 'p.*',
                                                 'from'     => array( 'posts' => 'p' ),
                                                 'where'    => $where,
                                                 'limit'         => array(0, 1),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'t.*',
                                                                                 'from'   => array( 'topics' => 't' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 'f.*',
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )

            );
        
        $this->ipsclass->DB->exec_query();	
        $page = $this->ipsclass->DB->fetch_row();
        //var_dump($page);exit();
        $page['rewrite_title'] = $this->common->filter_title($page['title']);
        $page['rewrite_name'] = $this->common->filter_title($page['name']);
        $page['post'] = preg_replace("#<!--emo&(.+?)-->.+?<!--endemo-->#", "\\1", $page['post']);
        
        if ($page['topic_hasattach']) {                    
                    $page['post'] = $this->class_attach->render_attachments($page['post'], $this->attach_pids);
        }
        $page['start_date']=$this->ipsclass->get_date($page['start_date'],"LONG");
        $this->output .= $this->ipsclass->compiled_templates['skin_home']->show_detail_page($page);
        unset ($page);
  
    }
    function showCartPage(){
        var_dump($this->ipsclass->cusFullName);exit();
        $this->carts = $_SESSION['cart'];
        
        if(empty ($this->carts['item'])  || count($this->carts['item']) <=0){
            $this->ipsclass->boink_it($this->ipsclass->vars['default_url']);
            return null;
        }
        $totalCartMoney = 0;
        $totalAmount = 0;
        $totalDiscountAmountMoney = 0;
        $totalOrderDiscount = 0;
        $strItem = "";
        $totalPay = 0;
        //var_dump($this->carts['item']);exit();
        $strItem .= $this->ipsclass->compiled_templates['skin_home']->show_top_cart_page($page);
        //duyet session cart
        //id: 
        //qty: so luong san pham
        foreach ($this->carts['item'] as $key => $item) {
                $where = "t.tid = {$item['id']}";
                $this->ipsclass->DB->flush_query();
                $this->ipsclass->DB->build_query( array( 'select'   => 't.*',
                                                         'from'     => array( 'topics' => 't' ),
                                                         'where'    => $where . "",
                                                         'limit'         => array(0, 1),
                                                         'add_join' => array(0 => array(
                                                                                         'select'=>'p.*',
                                                                                         'from'   => array( 'posts' => 'p' ),
                                                                                         'where'    => 'p.topic_id = t.tid',
                                                                                         'type'   => 'left'
                                                                                        ),
                                                                             1 => array( 'select' => 'f.*',
                                                                                          'from'   => array( 'forums' => 'f' ),
                                                                                          'where'  => 'f.id=t.forum_id',
                                                                                          'type'   => 'left' ),
                                                                             2 => array( 'select' => 'at.*',
                                                                                          'from'   => array( 'attachments' => 'at' ),
                                                                                          'where'  => 'at.attach_rel_id=p.pid ',
                                                                                          'limit'  => array(0,1),
                                                                                          'type'   => 'left' ),//wg_
                                                                            3 => array( 'select' => 'pro.*',
                                                                                          'from'   => array( 'product_info' => 'pro' ),
                                                                                          'where'  => 't.tid=pro.product_id ',
                                                                                          'limit'  => array(0,1),
                                                                                          'type'   => 'left' )
                                                             )
                                                        )

                    );
                $this->ipsclass->DB->exec_query();
                $row = $this->ipsclass->DB->fetch_row();
                $row['qty'] = $item['qty'];
                $item1 = $this->common->getByID(intval( $row['pid']),'product_info','product_id');
                
                //var_dump($item);exit();
                $temp = "";
                if(intval($item1['quantity']) > 1){
                    
                    for ($index = 1; $index <= $item1['quantity']; $index++) {
                        $module['value'] = $index;
                        $module['current_qty'] = $row['qty'];
                        $temp .= $this->ipsclass->compiled_templates['skin_home']->load_quantity_item_module ($module);
                    }
                    
                }else{
                    $module['value'] = 1;
                    $module['disable'] = TRUE;
                    $temp .= $this->ipsclass->compiled_templates['skin_home']->load_quantity_item_module ($module);
                }
                unset ($module);
                $module['pid'] = $row['pid']; 
                $module['option_qty'] = $temp;
                $row['option_qty'] = $this->ipsclass->compiled_templates['skin_home']->load_quantity_module ($module);
                unset ($module);
                //var_dump($row['option_qty']) ;exit();
                $row['totalMoney'] = intval($row['org_price']) * intval($item['qty']);
                $row['totalDiscountMoney'] = $row['price'] * intval($item['qty']);
                $row['rewrite_date'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['post_date']);
                $row['org_price'] = number_format($row['org_price']);
                $row['price'] = number_format($row['price']);
                //$page = $row;
                $module = $row;
                $totalAmount += intval($item['qty']);
                $totalCartMoney += intval($row['totalMoney']);
                $totalDiscountAmountMoney += intval($row['totalDiscountMoney']);
                $strItem .= $this->ipsclass->compiled_templates['skin_home']-> load_cart_item_module($module);
                //var_dump($row['qty']);exit();
                //$strItem .= $this->ipsclass->compiled_templates['skin_home']->show_cart_page($page);
                
                $page = null;
                //
            //}
        }
        //exit();
        //echo $totalAmount;
        if(intval($this->ipsclass->member['id']) > 0){
            $this->ipsclass->member['cusFullName'] = $this->ipsclass->member['field_14']. " " . ($this->ipsclass->member['field_20'] != "" ? ($this->ipsclass->member['field_20'] . " "): "")  . $this->ipsclass->member['field_15'];
            //var_dump($page['CusFullName']);exit();
        }
        if(intval($this->ipsclass->member['id']) > 0 && intval($this->ipsclass->member['field_17']) > 0){
            $totalOrderDiscount += $totalAmount * $this->ipsclass->vars['new_member_discount_money'];
            $totalPay = $totalDiscountAmountMoney - $totalOrderDiscount;
        }else{
            $totalPay = $totalDiscountAmountMoney;
        }
        //var_dump($totalAmount);exit();
        $page['totalOrderDiscount'] = number_format( $totalOrderDiscount);
        $page['totalCartMoney'] = number_format($totalCartMoney);
        $page['totalDiscountAmountMoney'] = number_format($totalDiscountAmountMoney);
        $page['totalAmount'] = number_format($totalAmount);
        $page['totalPay'] = number_format($totalPay);
        $strItem .= $this->ipsclass->compiled_templates['skin_home']->show_bottom_cart_page($page);
        $strItem .= $this->ipsclass->compiled_templates['skin_home']->load_buyer_info_module($module);
        //$this->output .= $this->ipsclass->compiled_templates['skin_home']->show_detail_page($page);
        //echo $strItem;
        unset ($page);
        $page['content'] = $strItem;
        $this->output .= $this->ipsclass->compiled_templates['skin_home']->show_cart_page($page);
//        var_dump(array(
//            'THE_LINK' => $this->base_url_nosess . "?act=Reg&CODE=03&uid=" . urlencode($member_id) . "&aid=" . urlencode($validate_key),
//            'NAME' => $member['members_display_name'],
//            'MAN_LINK' => $this->base_url_nosess . "?act=Reg&CODE=05",
//            'EMAIL' => $member['email'],
//            'ID' => $member_id,
//            'CODE' => $validate_key,
//                ));exit();
    }
    function paymentPage(){
        
        require ROOT_PATH . "sources/classes/class_email.php";

        $this->email = new emailer();
        $this->email->ipsclass = & $this->ipsclass;
        $this->email->email_init();
        $flag = FALSE;
        //$lay du lieu gui len ser ver:
        //
        //$this->ipsclass->input
        //
        //vi du lay name: $this->ipsclass->input['name']
        $this->carts = $_SESSION['cart'];
        $inEmail = $this->ipsclass->clean_email(strtolower(trim($this->ipsclass->input['payEmail'])));
        $name = trim($this->ipsclass->input['payRealName']);
        //var_dump($this->ipsclass->input);
        //exit() ;
        
        
        if(empty ($this->carts['item'])  || count($this->carts['item']) <=0){
            return null;
        }
         $order = array(
                'cust_name' => $name,
                'cust_address' => $this->ipsclass->input['payArea'],
                'cust_email' => $inEmail, // Private added by BabyWolf
                'cust_phone' => $this->ipsclass->input['payPhone'], // Buddy property added by BabyWolf
                'order_date' => time(),        
             );
         
         #Inser du lieu vao database
        $this->ipsclass->DB->do_insert('product_order', $order);
        
        $orderId =  $this->ipsclass->DB->get_insert_id();
        
        #Duyet session cart de insert vao bang order detail 
        
        foreach ($this->carts['item'] as $key => $item) {
            //$item['id']
            //$item['qty']
            
             $orderDetail = array(
                'order_id' => $orderId,
                'product_id' => $item['id'],
                'product_qty' => $item['qty'],
             );
             $this->ipsclass->DB->do_insert('product_order_detail', $orderDetail);
        
             $orderDetailID =  $this->ipsclass->DB->get_insert_id();                        
             
             if($orderDetailID > 0)
             {
                 $flag = TRUE;
             }
        }
        //send mail
        //-----------------------------------------
        // Get the emailer module
        //-----------------------------------------

        
        $this->email->get_template("payment_message");

        
        $this->email->build_message(array(
            'THE_LINK' => '',//$this->base_url_nosess . "?act=Reg&CODE=03&uid=" . urlencode($member_id) . "&aid=" . urlencode($validate_key),
            'NAME' => $name,
            'MAN_LINK' => '',//$this->base_url_nosess . "?act=Reg&CODE=05",
            'EMAIL' => $inEmail,
            'ID' => '',//$member_id,
            'CODE' => '',//$validate_key,
                )
        );

        $this->email->subject = $this->ipsclass->lang['new_registration_email'] . $this->ipsclass->vars['board_name'];
        $this->email->to = $inEmail;
        $this->email->message ="abc";
        $this->email->send_mail();
        
        
       //Huy session
        $this->carts = null;
        $_SESSION['cart'] = null;
        //Chuyen ve trang chu
        //$this->ipsclass->boink_it( $this->ipsclass->vars['default_url']);
        
        echo $flag;
        //var_dump($this->ipsclass->input);
        exit();
    }
    
    function showOrderPage(){
        $where = "1=1; ";
        $totalCartMoney = 0;
        $totalAmount = 0;
        $totalDiscountAmountMoney = 0;
        $totalOrderDiscount = 0;
        $totalPay = 0;
        $page = array();
        $this->ipsclass->DB->simple_construct(array('select'    => 'p.*',
                                                        'from'		=> 'product_order p order by p.order_id DESC',
                                                        )
                                                );
        $this->ipsclass->DB->simple_exec();
        $temp = array();
        while($row = $this->ipsclass->DB->fetch_row()){
            $row['order_date'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['order_date']);
            $detail = $this->loadOrderDetail(intval($row['order_id']));
            $row['detail'] = $detail;
            $row['countDetail'] = count($detail['totalData']);
            $temp[] = $row;
        }
        //var_dump(count($temp));exit();
        $page['totalData'] = count($temp);
        $page['datas'] = $temp;
        //var_dump ($this->ipsclass->compiled_templates['skin_home']->show_order_page($page));exit();
        
        $this->output .= $this->ipsclass->compiled_templates['skin_home']->show_order_page($page);
        
    }
    function loadOrderDetail($orderID){
        $datas = array();
        $where = "pt.order_id = {$orderID} ";
        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => 'pt.*',
                                                 'from'     => array( 'product_order_detail' => 'pt' ),
                                                 'where'    => $where . "",
                                                 //'limit'         => array($start, $perPage),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'p.*',
                                                                                 'from'   => array( 'posts' => 'p' ),
                                                                                 'where'    => 'p.topic_id = pt.product_id',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 't.*',
                                                                                      'from'   => array( 'topics' => 't' ),
                                                                                      'where'  => 'p.topic_id=t.tid',
                                                                                      'type'   => 'left' ),
                                                                      2 => array( 'select' => 'pi.*',
                                                                                      'from'   => array( 'product_info' => 'pi' ),
                                                                                      'where'  => 't.tid=pi.product_id',
                                                                                      'type'   => 'left' ),
                                                 )
                                            )
                                        );
        $temp = array();
        $this->ipsclass->DB->exec_query();
        while($row = $this->ipsclass->DB->fetch_row()){
            //$row['total_price'] = number_format(intval($row['price']) * intval($row['product_qty']));
            $row['totalMoney'] = number_format(intval($row['org_price']) * intval($row['product_qty']));
            $row['totalDiscountMoney'] = number_format($row['price'] * intval($row['product_qty']));
            $row['price'] = number_format( $row['price']);
            $row['org_price'] = number_format( $row['org_price']);
            $row['product_price'] = number_format( $row['product_price']);
            $temp[] = $row;
            
            $totalAmount += intval($row['product_qty']);
            $totalCartMoney += intval($row['totalMoney']);
            $totalDiscountAmountMoney += intval($row['totalDiscountMoney']);
            
            
        }
        if(intval($this->ipsclass->member['id']) > 0 && intval($this->ipsclass->member['field_17']) > 0){
            $totalOrderDiscount += $totalAmount * $this->ipsclass->vars['new_member_discount_money'];
            $totalPay = $totalDiscountAmountMoney - $totalOrderDiscount;
        }else{
            $totalPay = $totalDiscountAmountMoney;
        }
        $datas['totalData'] = count($temp);
        $datas['datas'] = $temp;
        $datas['totalOrderDiscount'] = number_format( $totalOrderDiscount);
        $datas['totalCartMoney'] = number_format($totalCartMoney);
        $datas['totalDiscountAmountMoney'] = number_format($totalDiscountAmountMoney);
        $datas['totalAmount'] = number_format($totalAmount);
        $datas['totalPay'] = number_format($totalPay);
        //var_dump($datas);exit();
        return $datas;
    }
    function init(){
        $this->ipsclass->load_language('lang_global');
        $this->ipsclass->load_language('lang_regportal');
        $this->ipsclass->load_template('skin_home');
        $this->ipsclass->load_language('lang_home');
        $this->act = $this->ipsclass->input["act"];
        $this->code = $this->ipsclass->input["CODE"];
        $this->hotPerPage = 2;
        $this->defaultPerPage = 2;
    }
    
    /*
     * end load page
     */
}
?>
